# Set default llvm-config names:
SET(LLVM_CONFIG_EXEC_NAMES
    "llvm-config" "llvm-config-3.3" "llvm-config-3.2" "llvm-config-3.1" "llvm-config-3.0"
    "llvm-config-2.9" "llvm-config-2.8")
# If the -DWITH_LLVM_CONFIG=... option is present:
IF(WITH_LLVM_CONFIG)
  SET(LLVM_CONFIG_EXEC_NAMES ${LLVM_CONFIG_EXEC_NAMES})
ENDIF(WITH_LLVM_CONFIG)
# search for llvm-config...
FIND_PROGRAM(LLVM_CONFIG_EXEC NAMES ${LLVM_CONFIG_EXEC_NAMES})


IF(LLVM_CONFIG_EXEC)
  SET(LLVM_CONFIG_FOUND TRUE)
  IF(WIN32)
    EXEC_PROGRAM(perl ARGS ${LLVM_CONFIG_EXEC} "--version"
      OUTPUT_VARIABLE LLVM_VERSION)
  ELSE(WIN32)
    EXEC_PROGRAM(${LLVM_CONFIG_EXEC} ARGS "--version"
      OUTPUT_VARIABLE LLVM_VERSION)
  ENDIF(WIN32)
ENDIF(LLVM_CONFIG_EXEC)


IF(LLVM_CONFIG_FOUND)
  # get linker flags
  IF(WIN32)
    EXEC_PROGRAM(perl ARGS ${LLVM_CONFIG_EXEC} "--ldflags"
      OUTPUT_VARIABLE LLVM_LD_FLAGS)
  ELSE(WIN32)
    EXEC_PROGRAM(${LLVM_CONFIG_EXEC} ARGS "--ldflags"
      OUTPUT_VARIABLE LLVM_LD_FLAGS)
  ENDIF(WIN32)    

  # get include dirs:
  IF(WIN32)  
    EXEC_PROGRAM(perl ARGS ${LLVM_CONFIG_EXEC} "--includedir"
      OUTPUT_VARIABLE LLVM_INCLUDE_DIR)
  ELSE(WIN32) 
    EXEC_PROGRAM(${LLVM_CONFIG_EXEC} ARGS "--includedir"
      OUTPUT_VARIABLE LLVM_INCLUDE_DIR)
  ENDIF(WIN32)

  # get lib dirs:
  IF(WIN32)
    EXEC_PROGRAM(perl ARGS ${LLVM_CONFIG_EXEC} "--libdir"
      OUTPUT_VARIABLE LLVM_LIB_DIR)
  ELSE(WIN32)
    EXEC_PROGRAM(${LLVM_CONFIG_EXEC} ARGS "--libdir"
      OUTPUT_VARIABLE LLVM_LIB_DIR)
  ENDIF(WIN32)
    
  # get libraries:
  IF(WIN32)
    EXEC_PROGRAM(perl ARGS ${LLVM_CONFIG_EXEC} "--libs engine interpreter"
      OUTPUT_VARIABLE LLVM_LIBRARY)
  ELSE(WIN32)
    EXEC_PROGRAM(${LLVM_CONFIG_EXEC} ARGS "--libs engine interpreter"
      OUTPUT_VARIABLE LLVM_LIBRARY)
  ENDIF(WIN32)

  IF(WIN32)
    EXEC_PROGRAM(perl ARGS ${LLVM_CONFIG_EXEC} "--cflags"
      OUTPUT_VARIABLE LLVM_C_FLAGS)
  ELSE(WIN32)
    EXEC_PROGRAM(${LLVM_CONFIG_EXEC} ARGS "--cflags"
      OUTPUT_VARIABLE LLVM_C_FLAGS)
  ENDIF(WIN32)

  IF(WIN32)
    EXEC_PROGRAM(perl ARGS ${LLVM_CONFIG_EXEC} "--cxxflags"
      OUTPUT_VARIABLE LLVM_CXX_FLAGS)
  ELSE(WIN32)
    EXEC_PROGRAM(${LLVM_CONFIG_EXEC} ARGS "--cxxflags"
      OUTPUT_VARIABLE LLVM_CXX_FLAGS)
  ENDIF(WIN32)

ENDIF(LLVM_CONFIG_FOUND)


#IF(STRING(REGEX MATCH "^([0-9]+).([0-9]+)"))
#  SET(LLVM_VERSION_NUMBER ${CMAKE_MATCH_0}*10+${CMAKE_MATCH_1})
#ENDIF(STRING(REGEX MATCH "^([0-9]+).([0-9]+)"))

INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LLVM DEFAULT_MSG
                                  LLVM_VERSION LLVM_INCLUDE_DIR LLVM_LIB_DIR LLVM_CXX_FLAGS
                                  LLVM_LIBRARY LLVM_LD_FLAGS)

# Parse version string to get numeric rep.


SET(LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIR})
if(SYSTEM_NAME MATCHES "FreeBSD")
  SET(LLVM_LIBRARIES ${LLVM_LIBRARY} m ffi)
else()
  SET(LLVM_LIBRARIES ${LLVM_LIBRARY} m ffi dl)
endif(SYSTEM_NAME MATCHES "FreeBSD")

IF(WIN32)
  SET(LLVM_LIBRARIES ${LLVM_LIBRARIES} imagehlp psapi)
ENDIF(WIN32)

mark_as_advanced(LLVM_VERSION LLVM_LIBRARY LLVM_INCLUDE_DIR LLVM_LIB_DIR LLVM_CXX_FLAGS LLVM_LD_FLAGS)
